import {defineStore} from 'pinia'
import request from '@/utils/request'
import { ApiRes,CategoryItem, TopCategory } from '@/types/data'
import { topCategory } from '../constants'
const defaultCategory = topCategory.map(item=>{
  return{
  name:item,
  id:item
  }
})
export default defineStore('category',{
  state: () => ({
    topCategory: {} as TopCategory,
    list:defaultCategory as CategoryItem[]
  }),
  actions:{
    async getCategoryList(){
      const res = await request.get<ApiRes<CategoryItem[]>>('/home/category/head')
      console.log(res);
      this.list = res.data.result
    },
    // 获取一级类目的信息
    async getTopCategory(id: string){
      // const res = await request.get<{msg: string, result:CategoryItem[]}>('/home/category/head')
      const res = await request.get<ApiRes<TopCategory>>('/category', {
        params: {id: id}
      })
      
      console.log('getTopCategory', res)
      this.topCategory = res.data.result
    },
  }
})

